package com.example.answer.androdi_db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
 * DBManager������װ�������е�ҵ�񷽷�
 * @author Administrator
 *
 */
public class DBManager1 {

	private DBHelper1 helper;
	private SQLiteDatabase db;

	public DBManager1(Context context) {
		helper = new DBHelper1(context);
		// ��ΪgetWritableDatabase�ڲ�������mContext.openOrCreateDatabase(mName, 0, mFactory);
		// ����Ҫȷ��context�ѳ�ʼ��,���ǿ��԰�ʵ����DBManager�Ĳ������Activity��onCreate��
		db = helper.getWritableDatabase();
	}
	/**
	 * add persons
	 * @param persons
	 */
	public void add(List<Person> persons) {
		db.beginTransaction(); // ��ʼ����
		try {
			for (Person person : persons) {
				db.execSQL("INSERT INTO person VALUES(null, ?, ?, ?)",
						new Object[] { person.name, person.age, person.info });
			}
			db.setTransactionSuccessful(); //��������ɹ����
		} finally {
			db.endTransaction(); // ��������
		}
	}
	/**
	 * ������Ա����
	 * @param person
	 */
	public void updateAge(Person person) {
		ContentValues cv = new ContentValues();
		cv.put("info", person.info);
		db.update("person", cv, "name = ?", new String[] { person.name });
	}
	/**
	 * ɾ����Ա
	 * @param person
	 */
	public void deleteOldPerson(Person person) {
		db.delete("person", "age >= ?", new String[] {String.valueOf(person.age) });
	}
	/**
	 *��ѯ������Ա,����List
	 * @return List<Person>
	 */
	public List<Person> query() {
		ArrayList<Person> persons = new ArrayList<Person>();
		Cursor c = queryTheCursor();
		while (c.moveToNext()) {
			Person person = new Person();
			person.id = c.getInt(c.getColumnIndex("id"));
			person.name = c.getString(c.getColumnIndex("name"));
			person.age = c.getInt(c.getColumnIndex("age"));
			person.info = c.getString(c.getColumnIndex("info"));
			persons.add(person);
		}
		c.close();
		return persons;
	}
	/**
	 * query all persons, return cursor
	 * @return Cursor
	 */
	public Cursor queryTheCursor() {
		Cursor c = db.rawQuery("SELECT * FROM person", null);
		return c;
	}

	/**
	 * close database
	 */
	public void closeDB() {
		db.close();
	}
}
